13. Solution: Which Match

Which Match - Solution

Given the following code:

int id = 4;
String idString = "" + id;
Uri uri = TaskContract.TaskEntry.CONTENT_URI;
uri = uri.buildUpon().appendPath(idString).build();
getContentResolver().delete(uri, null, null);

The constructed URI will be: content://com.example.android.todolist/tasks/4.
The URIMatcher will recognize this URI as identifying a single row in the tasks database with a row id = 4, so it will match it to the TASK_WITH_ID integer constant, which is the correct answer.

Without this appended ID, this would match with the TASKS constant.

Next, let’s go through the details of the delete function call via a ContentResolver.

getContentResolver().delete(uri, null, null);

This delete call will get to our TaskContentProvider and then the UriMatcher will match the passed in URI to the TASK_WITH_ID integer constant. This is important for the delete method because the ContentProvider needs to be able to identify and delete just the one row with ID = 4. It shouldn’t delete the entire directory of tasks.

Example of different delete behavior depending on the recognized URI.

Example of different delete behavior depending on the recognized URI.

So, when a ContentProvider works with multiple types of data and corresponding URI’s, a UriMatcher plays an important role in accurate data management.